<?php
/**
 * Created by PhpStorm.
 * User: 贡7
 * Date: 2018/2/5
 * Time: 下午4:47
 */

namespace app\index\controller;

use app\index\model\ActivityComment;
use app\index\model\ActivityPeople;

class Users extends Base
{
    /**
     * 个人中心我的活动列表
     * @return string
     */
    public function getList()
    {
        $activity_comment_model = new ActivityComment();
        $openid = session('openid');
        $time = time();
        $type_one = $activity_comment_model->alias('ac')
            ->join('activity a', 'a.id = ac.activity_id')
            ->field('ac.activity_id,ac.status,ac.money,ac.id,a.start_time,a.end_time,a.name,a.front_cover')
            ->where(['ac.status' => ['>', 0],'openid' => $openid])
            ->order("ac.id desc")
            ->limit(6)
            ->select();
        if ($type_one){
            foreach ($type_one as $k => $v){
                if ($time < $v['start_time']){
                    $day = ceil(($v['start_time'] - $time)/(3600 * 24));
                    $type_one[$k]['reminder'] = "距活动开始还有".$day."天";
                }elseif ($time < $v['end_time']){
                    $type_one[$k]['reminder'] = "活动进行中";
                }else{
                    $type_one[$k]['reminder'] = "活动已结束";
                }
                $type_one[$k]['start_time'] = date('Y.m.d');
                $type_one[$k]['end_time'] = date('Y.m.d');
            }
        }
        $type_two =  $activity_comment_model->alias('ac')
            ->join('activity a', 'a.id = ac.activity_id')
            ->field('ac.activity_id,ac.status,ac.money,ac.id,a.start_time,a.end_time,a.name,a.front_cover')
            ->where(['ac.status' => 1,'openid' => $openid])
            ->order("ac.id desc")
            ->limit(6)
            ->select();
        if ($type_two){
            foreach ($type_two as $k => $v){
                if ($time < $v['start_time']){
                    $day = ceil(($v['start_time'] - $time)/(3600 * 24));
                    $type_two[$k]['reminder'] = "距活动开始还有".$day."天";
                }elseif ($time < $v['end_time']){
                    $type_two[$k]['reminder'] = "活动进行中";
                }else{
                    $type_two[$k]['reminder'] = "活动已结束";
                }
                $type_two[$k]['start_time'] = date('Y.m.d');
                $type_two[$k]['end_time'] = date('Y.m.d');
            }
        }
        $type_three =  $activity_comment_model->alias('ac')
            ->join('activity a', 'a.id = ac.activity_id')
            ->field('ac.activity_id,ac.status,ac.money,ac.id,a.start_time,a.end_time,a.name,a.front_cover')
            ->where(['ac.status' => ['>',1],'openid' => $openid])
            ->order("ac.id desc")
            ->limit(6)
            ->select();
        if ($type_three){
            foreach ($type_three as $k => $v){
                if ($time < $v['start_time']){
                    $day = ceil(($v['start_time'] - $time)/(3600 * 24));
                    $type_three[$k]['reminder'] = "距活动开始还有".$day."天";
                }elseif ($time < $v['end_time']){
                    $type_three[$k]['reminder'] = "活动进行中";
                }else{
                    $type_three[$k]['reminder'] = "活动已结束";
                }
                $type_three[$k]['start_time'] = date('Y.m.d');
                $type_three[$k]['end_time'] = date('Y.m.d');
            }
        }
        $this->assign('list',json_encode([$type_one,$type_two,$type_three]));
       //return json_encode([$type_one,$type_two,$type_three]);
        return $this->fetch();
    }

    /**
     * 下拉刷新
     * @param len 长度
     * @param type 类型
     * @return mixed|string
     */
    public function getMoreList(){
        $length = input('len',0);
        $type = input('type',0);
        $time = time();
        $activity_comment_model = new ActivityComment();
        $openid = session('openid');
        switch ($type){
            case 1:
                $list = $activity_comment_model->alias('ac')
                    ->join('activity a', 'a.id = ac.activity_id')
                    ->field('ac.activity_id,ac.status,ac.money,ac.id,a.start_time,a.end_time,a.name,a.front_cover')
                    ->where(['ac.status' => ['>', '0'],'openid' => $openid])
                    ->order("ac.id desc")
                    ->limit($length,6)
                    ->select();
                break;
            case 2:
                $list = $activity_comment_model->alias('ac')
                    ->join('activity a', 'a.id = ac.activity_id')
                    ->field('ac.activity_id,ac.status,ac.money,ac.id,a.start_time,a.end_time,a.name,a.front_cover')
                    ->where(['ac.status' => 1,'openid' => $openid])
                    ->order("ac.id desc")
                    ->limit($length,6)
                    ->select();
                break;
            case 3:
                $list = $activity_comment_model->alias('ac')
                    ->join('activity a', 'a.id = ac.activity_id')
                    ->field('ac.activity_id,ac.status,ac.money,ac.id,a.start_time,a.end_time,a.name,a.front_cover')
                    ->where(['ac.status' => ['>',1],'openid' => $openid])
                    ->order("ac.id desc")
                    ->limit($length,6)
                    ->select();
                break;
        }
        if ($list){
            foreach ($list as $k => $v){
                if ($time < $v['start_time']){
                    $day = ceil(($v['start_time'] - $time)/(3600 * 24));
                    $list[$k]['reminder'] = "距活动开始还有".$day."天";
                }elseif ($time < $v['end_time']){
                    $list[$k]['reminder'] = "活动进行中";
                }else{
                    $list[$k]['reminder'] = "活动已结束";
                }
                $list[$k]['start_time'] = date('Y.m.d');
                $list[$k]['end_time'] = date('Y.m.d');
            }
        }

        return json_encode($list);
    }

    /**
     * 活动详情
     * @param id 活动id
     * @return mixed
     */
    public function detail(){
        $id = input('id');
        $time = time();
        $activity_comment_model = new ActivityComment();
        $activity_people_model = new ActivityPeople();
        $info = $activity_comment_model->alias('ac')
            ->join('activity a', 'a.id = ac.activity_id')
            ->field('ac.activity_id,ac.status,ac.money,ac.pay_time,ac.refund_time,ac.out_trade_no,ac.name,ac.mobile,ac.remark,ac.jylsh,ac.id,a.start_time,a.end_time,a.name as names,a.front_cover')
            ->where(['ac.id' => $id ])
            ->find();
        if ($info){
            if ($time < $info['start_time']){
                $day = ceil(($info['start_time'] - $time)/(3600 * 24));
                $info['reminder'] = "距活动开始还有".$day."天";
            }elseif ($time < $info['end_time']){
                $info['reminder'] = "活动进行中";
            }else{
                $info['reminder'] = "活动已结束";
            }
            if ($info['pay_time']){
                $info['pay_time'] = date("Y-m-d H:i:s",$info['pay_time']);
            }
            $info['start_time'] = date('Y.m.d',$info['start_time']);
            $info['end_time'] = date('Y.m.d',$info['end_time']);
            $info['trip'] = $activity_people_model->where(['comment_id' => $info['id'],'status' => 2])->select();
        }
        //return json($info);
        $this->assign('info',json_encode($info));

        return $this->fetch();
    }
}